package org.richin.collection.util;

import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class MapUtils {
	/**
	   * Ermittlung des ersten Key-Objektes der
	   * Übergebenen Map.
	   *
	   * @param pMap
	   * @return das erste Key-Objekt
	   */
	public static final <K, V> K getFirstKey(final Map<K, V> pMap) {
		final Set<K> keySet = pMap.keySet();
		for (K k : keySet)
			return k;
		return null;
	}
	/**
	 * 创建一个MAP，用一个LIST的值作为其键名，另一个LIST的值作为其值
	 * @param <K> 
	 * @param <V> 
	 * @param keys 存放键名的List
	 * @param values 存放值的List
	 * @return
	 * @throws IllegalAccessException
	 */
	public static  <K, V> Map<K, V> combine(final List<K> keys, final List<V> values)
			throws IllegalAccessException {
		if (CollectionUtils.isEmpty(keys) && CollectionUtils.isEmpty(values))
			return Collections.emptyMap();
		if (keys.size() != values.size())
			throw new IllegalAccessException("长度不相同");
		Map<K, V> resultMap = new HashMap<K, V>(keys.size());
		for (int i = 0; i < keys.size(); i++) {
			resultMap.put(keys.get(i), values.get(i));
		}
		return resultMap;
	}

	public static <K, V> int size(final Map<K, V> map) {
		return map == null ? 0 : map.size();
	}

	public static <K, V> boolean isEmpty(final Map<K, V> map) {
		return size(map) == 0;
	}

	public static <K, V> Map<K, V> nonNullMap(final Map<K, V> map) {
		if (map == null)
			return Collections.emptyMap();

		return map;
	}

	final static public <K, V> K GetFirstEntryKey(Map<K, V> map) {
		if ((map == null) || map.isEmpty())
			return null;
		return GetFirstEntry(map).getKey();
	}

	final static public <K, V> V GetFirstEntryValue(Map<K, V> map) {
		if ((map == null) || map.isEmpty())
			return null;
		return GetFirstEntry(map).getValue();
	}

	final static public <K, V> Map.Entry<K, V> GetFirstEntry(Map<K, V> map) {
		if ((map == null) || map.isEmpty())
			return null;
		return map.entrySet().iterator().next();
	}

	@SuppressWarnings("unchecked")
	final static public Map FilterEntries(Map map, Class clazz) {
		Map resultMap = new HashMap(map.size());
		if ((map == null) || map.isEmpty()) {
			return resultMap;
		}
		Iterator entries = map.entrySet().iterator();
		while (entries.hasNext()) {
			Map.Entry entry = (Map.Entry) entries.next();
			if (clazz.isInstance(entry.getValue())) {
				resultMap.put(entry.getKey(), entry.getValue());
			}
		}
		return resultMap;
	}

	public static String optionListFromMap(List<Map<String, Object>> list, String keyField, String displayField) {
		return optionListFromMap(list, keyField, displayField, null);
	}

	public static String optionListFromMap(List<Map<String, Object>> items, String keyField, String displayField,
			String defaultValue) {

		StringBuffer list = new StringBuffer("");
		try {
			Iterator<Map<String, Object>> iter = items.iterator();
			while (iter.hasNext()) {
				Map<String, Object> mapobj = iter.next();
				list.append("<OPTION ");
				String mapval = "" + mapobj.get(keyField);
				if (defaultValue != null) {
					if (mapval != null && mapval.equals(defaultValue)) {
						list.append("SELECTED ");
					} // end if
				} // end if
				list.append("VALUE='" + mapval + "'>");
				list.append((String) mapobj.get(displayField));
				list.append("</OPTION>\n");
			}
		} catch (Exception e) {
			list.append("#Parse Error " + e.getMessage() + "#");
		}

		return list.toString();

	}

}
